package com.zzb.learning.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzb.base.model.PageResult;
import com.zzb.learning.mapper.XcChooseCourseMapper;
import com.zzb.learning.mapper.XcCourseTablesMapper;
import com.zzb.learning.model.dto.MyCourseTableParams;
import com.zzb.learning.model.dto.XcCourseTablesDto;
import com.zzb.learning.model.po.XcChooseCourse;
import com.zzb.learning.model.po.XcCourseTables;
import com.zzb.learning.service.CourseTablesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @Author zzb
 * @Date 2024/7/11 11:08
 * @Version 1.0
 * @description
 */
@Service
@Slf4j
public class CourseTablesServiceImpl extends ServiceImpl<XcCourseTablesMapper, XcCourseTables> implements CourseTablesService {
    @Autowired
    private XcCourseTablesMapper xcCourseTablesMapper;
    @Autowired
    private XcChooseCourseMapper xcChooseCourseMapper;

    @Override
    public XcCourseTablesDto getLearnstatus(String userId, Long courseId) {
        //查询我的课程表
        XcCourseTables xcCourseTables = xcCourseTablesMapper.selectOne(new QueryWrapper<XcCourseTables>().eq("course_id", courseId));
        //学习资格
        // [{"code":"702001","desc":"正常学习"},
        // {"code":"702002","desc":"没有选课或选课后没有支付"},
        // {"code":"702003","desc":"已过期需要申请续期或重新支付"}]
        XcCourseTablesDto xcCourseTablesDto = new XcCourseTablesDto();
        if (xcCourseTables == null) {
            //没有选课或选课后没有支付
            xcCourseTablesDto.setLearnStatus("702002");
            return xcCourseTablesDto;
        }
        BeanUtils.copyProperties(xcCourseTables, xcCourseTablesDto);
        //是否过期，true过期，false未过期
        boolean isExpires = xcCourseTables.getValidTimeEnd().isBefore(LocalDateTime.now());
        if (!isExpires) {
            //正常学习
            xcCourseTablesDto.setLearnStatus("702001");
        } else {
            //已过期
            xcCourseTablesDto.setLearnStatus("702003");
        }
        return xcCourseTablesDto;
    }

    @Override
    public boolean saveChooseCourseStatus(String chooseCourseId) {
        //根据chooseCourseId查询该选课
        XcChooseCourse chooseCourse = xcChooseCourseMapper.selectById(chooseCourseId);
        if (chooseCourse == null) {
            log.info("该课程不在选课范围中......, {}", chooseCourseId);
            return false;
        }
        //创建XcCourseTables对象
        XcCourseTables xcCourseTables = new XcCourseTables();
        //拷贝对象
        BeanUtils.copyProperties(chooseCourse, xcCourseTables);
        xcCourseTables.setChooseCourseId(chooseCourse.getId());
        xcCourseTables.setUpdateDate(LocalDateTime.now());
        int insert = xcCourseTablesMapper.insert(xcCourseTables);
        if (insert <= 0) {
            return false;
        }
        return true;
    }

    @Override
    public PageResult<XcCourseTables> courseTable(MyCourseTableParams params) {
        LambdaQueryWrapper<XcCourseTables> queryWrapper = new LambdaQueryWrapper<XcCourseTables>()
                .eq(XcCourseTables::getUserId,params.getUserId());
        Page<XcCourseTables> page = new Page<>(params.getPage(), params.getSize());
        Page<XcCourseTables> courseTablesPage = xcCourseTablesMapper.selectPage(page, queryWrapper);
        PageResult<XcCourseTables> pageResult = new PageResult<>(courseTablesPage.getRecords(),courseTablesPage.getTotal(),
                courseTablesPage.getPages(),courseTablesPage.getSize());
        return pageResult;
    }
}
